﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient; 

namespace Administrare
{
    public partial class UserRolesFrm : FrmRezumat
    {
        private Int32 idxBInfoUser = 0;
        private Int32 idxBAccesMenu = 0;
        public UserRolesFrm(ModuleClass mInfo)
            : base(mInfo)
        {
            InitializeComponent();
            workConnString = "AdminConnectionString";
            adminDataSet.aspnet_Roles.LoweredRoleNameColumn.DefaultValue = String.Empty;
            adminDataSet.aspnet_Users.LoweredUserNameColumn.DefaultValue = String.Empty;
            adminDataSet.aspnet_Users.IsAnonymousColumn.DefaultValue = false;
            adminDataSet.aspnet_Users.LastActivityDateColumn.DefaultValue = DateTime.Now;
            this.HandleAfterAdd += new AfterAddHandler(UserRolesFrm_HandleAfterAdd);
        }

        void UserRolesFrm_HandleAfterAdd(object sender, DataGridViewCellEventArgs args)
        {
            //fKaspnetRoAppliBS.MoveNext();
            
            if (fKaspnetRoAppliBS.Current != null)
            {
                AdminDataSet.aspnet_RolesRow rr = (AdminDataSet.aspnet_RolesRow)(((DataRowView)fKaspnetRoAppliBS.Current).Row);
                if (rr != null)
                {
                    Guid g;
                    // Create and display the value of two GUIDs.
                    g = Guid.NewGuid();
                    rr.RoleId = g;
                    Boolean gasit = false;
                    while (!gasit)
                    {
                        gasit = true;
                        foreach (DataRowView dv in aspnetRolesBindingSource)
                        { 
                            AdminDataSet.aspnet_RolesRow ret = dv.Row as AdminDataSet.aspnet_RolesRow;
                            if (ret != null)
                            {
                                if (ret.RoleId == rr.RoleId)
                                {
                                    gasit = false;
                                    rr.RoleId = Guid.NewGuid();
                                    break;
                                }
                                    
                            }
                        }

                    }
                }

            }
        }

       
        private void UserRolesFrm_Load(object sender, EventArgs e)
        {
            AddBindingSource(aspnet_UsersBindingSource);
            AddBindingSource(fKaspnetRoAppliBS);
            AddBindingSource(fKaspnetUsRoleBS);
            AddBindingSource(admin_RoleInApplicationsBindingSource);

            listTableAdaptor.Add(aspnet_UsersTableAdapter.Adapter);
            listTableAdaptor.Add(aspnet_RolesTableAdapter.Adapter);
            listTableAdaptor.Add(aspnet_UsersInRolesTableAdapter.Adapter);
            listTableAdaptor.Add(admin_RoleInApplicationsTableAdapter.Adapter);


            SqlConnection con = DeschideConexiune(workConnString);
            this.admin_RoleInApplicationsTableAdapter.Connection = con;
            this.admin_RoleInApplicationsTableAdapter.Fill(this.adminDataSet.admin_RoleInApplications);
            this.aspnet_UsersInRolesTableAdapter.Connection = con;
            this.aspnet_UsersInRolesTableAdapter.Fill(this.adminDataSet.aspnet_UsersInRoles);

            this.aspnet_UsersInRolesTableAdapter.Connection = con;
            this.aspnet_UsersInRolesTableAdapter.Fill(this.adminDataSet.aspnet_UsersInRoles);
            this.aspnet_UsersTableAdapter.Connection = con;
            this.aspnet_UsersTableAdapter.Fill(this.adminDataSet.aspnet_Users);
            this.aspnet_RolesTableAdapter.Connection = con;
            this.aspnet_RolesTableAdapter.Fill(this.adminDataSet.aspnet_Roles);
            this.aspnet_ApplicationsTableAdapter.Connection = con;
            this.aspnet_ApplicationsTableAdapter.Fill(this.adminDataSet.aspnet_Applications);
            InchideConexiune(con);
            aspnet_ApplicationsBindingSource.Filter = "[LoweredApplicationName]='administrare'";
            if (aspnet_ApplicationsBindingSource.Count == 0)
                MessageBox.Show("Atentie! Modulul de administrare nu a fost instalat!");
            IndexSetCurent = 0;
            idxBInfoUser = AddButton("Info Utilizator ", global::ERPStudio.Properties.Resources.info, Membership, ToolStripItemAlignment.Right, false);
            idxBAccesMenu = AddButton("Acces Meniuri", global::ERPStudio.Properties.Resources.info, AccessMenu, ToolStripItemAlignment.Right, false);
        }

        private void Membership(object sender, EventArgs e)
        {
            if (((DataRowView)aspnet_UsersBindingSource.Current).IsNew)
            {
                MessageBox.Show("Va rugam selectati un utilizator salvat in baza de date!\nUtilizatorul curent nu a fost salvat inca!", "Atentie");
                return;
            }
            AdminDataSet.aspnet_UsersRow uRow = ((DataRowView)aspnet_UsersBindingSource.Current).Row
                as AdminDataSet.aspnet_UsersRow;
            MembershipFrm frm = new MembershipFrm(uRow.UserId,ModuleInfo);
            frm.MdiParent = MdiParent;
            frm.Show();
        }

        private void AccessMenu(object sender, EventArgs e)
        {

            if (((DataRowView)admin_RoleInApplicationsBindingSource.Current).IsNew)
            {
                MessageBox.Show("Va rugam selectati o aplicatie setata si salvata in baza de date!\nAplicatia curenta nu a fost salvata inca!", "Atentie");
                return;
            }
            AdminDataSet.admin_RoleInApplicationsRow uRow = ((DataRowView)admin_RoleInApplicationsBindingSource.Current).Row
                as AdminDataSet.admin_RoleInApplicationsRow;
            AccessMenuFrm frm = new AccessMenuFrm(uRow.ApplicationId,uRow.RoleId,ModuleInfo);
            frm.MdiParent = MdiParent;
            frm.Show();
        }


        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (UsersRolesTC.SelectedTab == UsersTP)
            {
                IndexSetCurent = 0;
                listTSButton[idxBInfoUser].Enabled = true;
            }
            else
                listTSButton[idxBInfoUser].Enabled = false;
            if (UsersRolesTC.SelectedTab == RolesTP)
                IndexSetCurent = 1;
            if (UsersRolesTC.SelectedTab == AppRolesTP)
            {
                listTSButton[idxBAccesMenu].Enabled = true;
            }
            else
                listTSButton[idxBAccesMenu].Enabled = false;
        }

        private void aspnet_UsersDataGridView_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
        {
            e.Row.Cells[UserIdColumn.Index].Value = Guid.NewGuid();
   
        }
        override public void Salvare(object sender, EventArgs args)
        {

            base.Salvare(this, args);
            if (EroareSalvare)
                return;
            /*AdminDataSet.aspnet_ApplicationsRow rd = ((DataRowView)aspnet_ApplicationsBindingSource.Current).Row as AdminDataSet.aspnet_ApplicationsRow;
            Guid cGui = rd.ApplicationId;
            SqlConnection con = DeschideConexiune(workConnString);
            this.aspnet_ApplicationsTableAdapter.Connection = con;
            this.aspnet_ApplicationsTableAdapter.Fill(this.adminDataSet.aspnet_Applications);
            InchideConexiune(con);
            int poz = aspnet_ApplicationsBindingSource.Find("ApplicationId", cGui);
            if (poz > 0)
                aspnet_ApplicationsBindingSource.Position = poz;
             * */
        }

       

        private void aspnet_UsersDGV_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (e.ColumnIndex == UserNameColumn.Index)
            {
                if (aspnet_UsersDGV[LoweredUserNameColumn.Index, e.RowIndex].Value.ToString()!=e.FormattedValue.ToString().ToLower())
                    aspnet_UsersDGV[LoweredUserNameColumn.Index, e.RowIndex].Value =
                        e.FormattedValue.ToString().ToLower();
            }
        }

        private void roluriDGV_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == roleNameDataGridViewTextBoxColumn.Index)
            {
                String newVal = roluriDGV[roleNameDataGridViewTextBoxColumn.Index, e.RowIndex].Value.ToString().ToLower();
                if(roluriDGV[loweredRoleNameDataGridViewTextBoxColumn.Index, e.RowIndex].Value.ToString() != newVal)
                    roluriDGV[loweredRoleNameDataGridViewTextBoxColumn.Index, e.RowIndex].Value = newVal;
            }
        }

      
        
    }

    [Serializable]
    public class TipAccessSursa
    {
        public TipAccessSursa(String nume, String _type)
        {
            _Name = nume;
            _Type = _type;
        }

        private String _Name = String.Empty;
        [System.ComponentModel.Bindable(true)]
        public String Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
            }
        }
        private String _Type = "T";
        [System.ComponentModel.Bindable(true)]
        public String Type
        {
            get
            {
                return _Type;
            }
            set
            {
                _Type = value;
            }
        }

    }
}
